package com.dries.portal.module.portal.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dries.common.api.CommonResult;
import com.dries.portal.module.portal.entity.Activity;
import com.dries.portal.module.portal.enums.ActivityEnum;
import com.dries.portal.module.portal.service.IActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 遐享活动表
 *
 * @author dries
 * @email samphsanie@gmail.com
 * @date 2020-08-06 10:50:19
 */
@Slf4j
@Api(tags = "遐享首页活动轮播API", value = "ActivityController")
@RestController
@RequestMapping("/portal/activity")
public class ActivityController {

    @Resource
    private IActivityService activityService;

    /**
     * 活动列表
     */
    @ApiOperation("轮播活动列表")
    @GetMapping("/list")
    public CommonResult list(){

        List<Activity> list = activityService.list(
                new QueryWrapper<Activity>().lambda().eq(Activity::getActivityStatus, ActivityEnum.ACTIVITY_ONLINE).or()
                .eq(Activity::getActivityStatus, ActivityEnum.ACTIVITY_COUPON)
        );

        log.info("【轮播活动列表】=======》 {}, 枚举: {}", JSONUtil.toJsonStr(list), list.get(0).getActivityStatus().getDescription());

        return CommonResult.success(list);
    }


    /**
     * 信息
     */
    @ApiOperation("根据活动ID查询活动")
    @GetMapping("/info/{activityId}")
    public CommonResult info(@PathVariable("activityId") Integer activityId){
		Activity activity = activityService.getById(activityId);

        return CommonResult.success(activity);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public CommonResult save(@RequestBody Activity activity){
		activityService.save(activity);

        return CommonResult.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public CommonResult update(@RequestBody Activity activity){
		activityService.updateById(activity);

        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public CommonResult delete(@RequestBody Integer[] activityIds){
		activityService.removeByIds(Arrays.asList(activityIds));

        return CommonResult.success();
    }

}
